/*
 * Copyright 2013 JROSE
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jrose.test.config;

import lombok.Cleanup;
import org.jrose.api.config.CommonConfig;
import org.jrose.api.config.ConfigManager;
import org.jrose.api.config.DataSourceConfig;
import org.jrose.api.config.MemcachedConfig;
import org.jrose.api.config.MongoConfig;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.InputStream;
import java.util.Properties;

public class MockConfigManager implements ConfigManager {
    private static final DataSourceConfig dataSourceConfig;
    private static final MongoConfig mongoConfig;

    static {
        dataSourceConfig = new DataSourceConfig();
        mongoConfig = new MongoConfig();

        try {
            Resource resource = new ClassPathResource("/ut.properties");
            @Cleanup InputStream inStream = resource.getInputStream();
            Properties props = new Properties();
            props.load(inStream);

            dataSourceConfig.setDriverClass(props.getProperty("datasource.driverClass"));
            dataSourceConfig.setJdbcUrl(props.getProperty("datasource.jdbcUrl"));
            dataSourceConfig.setUsername(props.getProperty("datasource.username"));
            dataSourceConfig.setPassword(props.getProperty("datasource.password"));
            dataSourceConfig.setPartitionCount(Integer.parseInt(props.getProperty("datasource.partitionCount")));
            dataSourceConfig.setMinConnectionsPerPartition(Integer.parseInt(props.getProperty("datasource.minConnectionsPerPartition")));
            dataSourceConfig.setMaxConnectionsPerPartition(Integer.parseInt(props.getProperty("datasource.maxConnectionsPerPartition")));
            dataSourceConfig.setAcquireIncrement(Integer.parseInt(props.getProperty("datasource.acquireIncrement")));
            dataSourceConfig.setPoolAvailabilityThreshold(Integer.parseInt(props.getProperty("datasource.poolAvailabilityThreshold")));
            dataSourceConfig.setConnectionTimeoutInMs(Integer.parseInt(props.getProperty("datasource.connectionTimeoutInMs")));
            dataSourceConfig.setReleaseHelperThreads(Integer.parseInt(props.getProperty("datasource.releaseHelperThreads")));
            dataSourceConfig.setStatementReleaseHelperThreads(Integer.parseInt(props.getProperty("datasource.statementReleaseHelperThreads")));
            dataSourceConfig.setIdleConnectionTestPeriodInMinutes(Integer.parseInt(props.getProperty("datasource.idleConnectionTestPeriodInMinutes")));
            dataSourceConfig.setIdleMaxAgeInMinutes(Integer.parseInt(props.getProperty("datasource.idleMaxAgeInMinutes")));
            dataSourceConfig.setStatementsCacheSize(Integer.parseInt(props.getProperty("datasource.statementsCacheSize")));
            dataSourceConfig.setConnectionTestStatement(props.getProperty("datasource.connectionTestStatement"));

            mongoConfig.setReplicaSet(props.getProperty("mongo.replicaSet"));
            mongoConfig.setConnectionsPerHost(Integer.parseInt(props.getProperty("mongo.connectionsPerHost")));
            mongoConfig.setThreadsAllowedToBlockForConnectionMultiplier(Integer.parseInt(props.getProperty("mongo.threadsAllowedToBlockForConnectionMultiplier")));
            mongoConfig.setConnectTimeout(Integer.parseInt(props.getProperty("mongo.connectTimeout")));
            mongoConfig.setMaxAutoConnectRetryTime(Integer.parseInt(props.getProperty("mongo.maxAutoConnectRetryTime")));
            mongoConfig.setMaxWaitTime(Integer.parseInt(props.getProperty("mongo.maxWaitTime")));
            mongoConfig.setAutoConnectRetry(Boolean.parseBoolean(props.getProperty("mongo.autoConnectRetry")));
            mongoConfig.setSocketKeepAlive(Boolean.parseBoolean(props.getProperty("mongo.socketKeepAlive")));
            mongoConfig.setSocketTimeout(Integer.parseInt(props.getProperty("mongo.socketTimeout")));
            mongoConfig.setSlaveOk(Boolean.parseBoolean(props.getProperty("mongo.slaveOk")));
            mongoConfig.setWriteNumber(Integer.parseInt(props.getProperty("mongo.writeNumber")));
            mongoConfig.setWriteTimeout(Integer.parseInt(props.getProperty("mongo.writeTimeout")));
            mongoConfig.setWriteFsync(Boolean.parseBoolean(props.getProperty("mongo.writeFsync")));
        } catch (Exception ex) {
            throw new RuntimeException(ex.getMessage(), ex);
        }
    }

    @Override
    public DataSourceConfig getDataSourceConfig(String name) {
        return dataSourceConfig;
    }

    @Override
    public MongoConfig getMongoConfig() {
        return mongoConfig;
    }

    @Override
    public MemcachedConfig getMemcachedConfig() {
        throw new UnsupportedOperationException();
    }

    @Override
    public CommonConfig getCommonConfig() {
        throw new UnsupportedOperationException();
    }
}
